package rs.in.idemdo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Spinner;
import android.widget.TextView;
import beans.TripLine;
import beans.TripPartBean;

public class IzabranaLinijaActivity extends Activity {

	private static  TripLine mTripLine;
	private ArrayList<HashMap<String, Object>> mListaDuziPutovanja = new ArrayList<HashMap<String, Object>>();

	private static final String STANICA = "1";
	private static final String PREVOZNIK = "2";
	private static final String CENA = "3";
	private static final String PREVOZNIK_IKONA = "4";
	private static final int IKONA_PREVOZNIKA = 0;
	private static final String VREMENA_POLASKA = null;

	private class SpinnerBinder implements ViewBinder {

		@Override
		public boolean setViewValue(View view, Object data,String textRepresentation) {

			if (view instanceof Spinner) {
				final Spinner spinner = (Spinner) view;
				ArrayAdapter<String> adapterOd = new ArrayAdapter<String>(getApplicationContext(), R.layout.izabrana_linija_vreme_spiner_stavka, (String[]) data);
				spinner.setAdapter(adapterOd);
			}
			if (view instanceof TextView)
			{
				final TextView textView = (TextView) view;
				textView.setText((String)data);
			}
			if (view instanceof ImageView)
			{
				final ImageView imageView = (ImageView) view;
				imageView.setImageResource((Integer) data);
			}
			
			return true;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.izabrana_linija);

		if (savedInstanceState == null) {
			Intent i = getIntent();
			Bundle extras = i
					.getBundleExtra(RezultatiPretrageActivity.TRIP_LINE);
			mTripLine = (TripLine) extras
					.get(RezultatiPretrageActivity.TRIP_LINE);
		}

		napuniArrayListu(mTripLine);
		adaptirajListView();
	}

	private void adaptirajListView() {
		final String[] from = new String[] { STANICA, PREVOZNIK,PREVOZNIK_IKONA, CENA, VREMENA_POLASKA };
		final int[] to = new int[] { R.id.txv_stanica, R.id.txv_prevoznik,R.id.iv_izab_linija_stavka_sredina_prevoznik, R.id.txv_cena,R.id.sp_vreme_polaska };

		final ListView lv = (ListView) findViewById(R.id.lv_lista_duzi_izabrane_linije);
		final SimpleAdapter sa = new SimpleAdapter(this, mListaDuziPutovanja,R.layout.izabrana_linija_stavka, from, to);
		sa.setViewBinder(new SpinnerBinder());
		lv.setAdapter(sa);
	}

	private void napuniArrayListu(TripLine tripLine) {
		// final lokalne promenljive radi najbrzeg pristupa
		final ArrayList<HashMap<String, Object>> lista = mListaDuziPutovanja;
		final int velicina = tripLine.getPutneDuzi().size();
		final Vector<TripPartBean> putneDuzi = tripLine.getPutneDuzi();

		ArrayList<String[]> lista_vremena = new ArrayList<String[]>();
		String[] array_spinner1 = new String[]{"10:00","10:30","11:00","11:30","12:00",};
		String[] array_spinner2 = new String[]{"12:30","13:00","14:00","15:30","16:00",};
		
		lista_vremena.add(array_spinner1);
		lista_vremena.add(array_spinner2);
		
		// final LayoutInflater factory = getLayoutInflater();
		// final View textEntryView =
		// factory.inflate(R.layout.izabrana_linija_stavka, null);
		// Spinner spVremePolaska = (Spinner)
		// textEntryView.findViewById(R.id.cbv_vreme_polaska);
		//
		// ArrayAdapter<String> adapterDo = new ArrayAdapter<String>(this,
		// R.layout.izabrana_linija_vreme_spiner_stavka, array_spinner);
		// spVremePolaska.setAdapter(adapterDo);

		for (int i = 0; i < velicina; i++) {
			HashMap<String, Object> mapa = new HashMap<String, Object>();
			mapa.put(STANICA, "MESTO: "
					+ putneDuzi.get(i).getMestoOd().getNaziv());
			mapa.put(PREVOZNIK, "PREVOZNIK: "
					+ putneDuzi.get(i).getPrevoznik().getNaziv());
			mapa.put(PREVOZNIK_IKONA,
					odrediIkonu(putneDuzi.get(i), IKONA_PREVOZNIKA));
			mapa.put(CENA, "CENA: " + putneDuzi.get(i).getCena());
			mapa.put(VREMENA_POLASKA, lista_vremena.get(i));
			lista.add(mapa);
		}
	}

	private Object odrediIkonu(TripPartBean putnaDuz, int tip) {
		switch (tip) {
		case IKONA_PREVOZNIKA:
			if (putnaDuz.getPrevoznik().getVrstaPrevoza().getNaziv()
					.equals("BUS")) {
				return R.drawable.bus;
			} else {
				return R.drawable.voz;
			}
		default:
			break;
		}
		return null;
	}
}
